package edu.gatech.cs2340.bytebankers.presenter;
import java.util.List;

import edu.gatech.cs2340.bytebankers.model.item.ItemManager;
import edu.gatech.cs2340.bytebankers.model.Journey;
import edu.gatech.cs2340.bytebankers.model.Leader;
import edu.gatech.cs2340.bytebankers.model.Store;
import edu.gatech.cs2340.bytebankers.model.Wagon;
import edu.gatech.cs2340.bytebankers.model.item.Item;
import edu.gatech.cs2340.bytebankers.notifications.GameEvents;
import edu.gatech.cs2340.bytebankers.notifications.Notifier;
import edu.gatech.cs2340.bytebankers.view.IStoreView;

/**
 * @author Huy N. Tran
 * 
 * Presenter class that helps manage the store through a view. 
 */
public class StorePresenter extends Notifier implements IStorePresenter 
{
	private IStoreView storeView;
	private Journey journey;
	private ItemManager itemManager;
	private Leader leader;
	private Wagon wagon;
	private Store store;
	
	private double totalPrice, totalWeight;
	private double[] price, weight;
	private int[] quant;
	private String[] itemName;
	private List<Item> itemList;
	
	public StorePresenter(Journey journey)
	{
		this.journey=journey;
		wagon=journey.getWagon();
		leader=journey.getLeader();
	}
	
	/**
	 * Method that deducts the appropriate amount of money from the leader and adds the 
	 * items purchased to the wagon.
	 */
	public void checkoutClicked() 
	{	
		itemList = itemManager.getAll();  
		int[] quanArr = storeView.getQuant();
		
		//summing up all the item prices
		for(int x=0; x<quanArr.length; x++)
			totalPrice += quanArr[x] * itemList.get(x).getBasePrice();
			
		//deducting the purchase amount from the leader's pocket
		leader.withdrawMoney(totalPrice);	
		
		//adding items to the wagon
		for(int x=0; x< quanArr.length; x++)
			wagon.addItemQuantity(itemList.get(x), quanArr[x]);
		
		System.out.println("Wagon available weight: " + wagon.getAvailableWeight());
		System.out.println("Leader remaining money: " + leader.getMoneyLeft());
		
		sendNotification(GameEvents.STORE_FINISHED);
	}

	/**
	 * Method that tells the store view the inventories to display.
	 */
	public void setView(IStoreView storeView) 
	{	
		this.storeView = storeView;
		
		storeView.setName("Indepence General Store"); //hard coded for right now
		storeView.setBankroll(leader.getMoneyLeft());
		storeView.setAvailableWeight(wagon.getAvailableWeight());
		
		itemManager = journey.getItemManager();
		itemList = itemManager.getAll();
		
		price = new double[itemList.size()];
		itemName = new String[itemList.size()];
		weight = new double[itemList.size()];
		quant = new int[itemList.size()];
		
		//Getting the items (their prices, names, weight, and quantity
		//that are available for purchase.
		for(int x=0; x<itemList.size(); x++)
		{
			price[x]=itemList.get(x).getBasePrice();
			itemName[x]=itemList.get(x).getName();
			weight[x]=itemList.get(x).getWeight();
			quant[x]=wagon.getItemQuantityRemaining(itemList.get(x));
		}
		//Sends the view double[] price, String[] name, int[] quantity, and double[] weight
		storeView.setInventory(price, itemName, quant, weight); 
	}

	/**
	 * Implemented method that is not necessarily need for this particular class.
	 */
	public void addPresenterListener(IPresenterListener listener) {}
}
